import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/auth/LoginView.vue'),
      meta: { requiresAuth: false }
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/auth/RegisterView.vue'),
      meta: { requiresAuth: false }
    },
    {
      path: '/',
      name: 'home',
      component: () => import('../views/general/DashboardView_fixed.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/users',
      name: 'users',
      component: () => import('../views/auth/UserManagementView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/password-management',
      name: 'password-management',
      component: () => import('../views/auth/PasswordManagementView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/role-permission-management',
      name: 'role-permission-management',
      component: () => import('../views/auth/RolePermissionManagementView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/courses/manage',
      name: 'course-management',
      component: () => import('../views/study/CourseManagementView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/system-config/logs',
      name: 'system-logs',
      component: () => import('../views/system/SystemLogsView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/system-config/status',
      name: 'system-status',
      component: () => import('../views/system/SystemStatusView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/profile',
      name: 'profile',
      component: () => import('../views/auth/ProfileView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/study/courses',
      name: 'study-courses',
      component: () => import('../views/study/CourseBrowseView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/study/courses/:id',
      name: 'study-course-detail',
      component: () => import('../views/study/CourseDetailView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/study/learning/:id',
      name: 'study-learning',
      component: () => import('../views/study/LearningView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/study/profile',
      name: 'study-profile',
      component: () => import('../views/study/StudyProfileView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/study/students/points',
      name: 'student-points-list',
      component: () => import('../views/study/StudentPointsListView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/study/questions',
      name: 'study-questions',
      component: () => import('../views/study/QuestionsView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/study/questions/ask',
      name: 'study-question-ask',
      component: () => import('../views/study/QuestionAskView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/study/questions/:id',
      name: 'study-question-detail',
      component: () => import('../views/study/QuestionDetailView.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/:pathMatch(.*)*',
      redirect: '/'
    }
  ]
})

// 添加路由守卫
import { hasRole, hasPermission } from '../utils/auth'

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')

  // 检查是否需要登录
  if (to.meta.requiresAuth && !token) {
    next('/login')
    return
  }

  // 已登录用户不能访问登录页
  if (to.name === 'login' && token) {
    next('/')
    return
  }

  // 检查页面权限
  if (to.meta.requiresAuth && token) {
    // 根据路由路径检查权限
    if (to.path === '/users' && !hasRole('ADMIN') && !hasPermission('USER_READ')) {
      next('/')
      return
    }

    if (to.path === '/password-management' && !hasRole('ADMIN') && !hasPermission('USER_UPDATE')) {
      next('/')
      return
    }

    if (to.path === '/projects' && !hasRole('ADMIN') && !hasPermission('PROJECT_READ')) {
      next('/')
      return
    }

    if (to.path.startsWith('/projects/') && !hasRole('ADMIN') && !hasPermission('PROJECT_READ')) {
      next('/')
      return
    }

    if (to.path === '/tasks' && !hasRole('ADMIN') && !hasPermission('TASK_READ')) {
      next('/')
      return
    }

    if (to.path === '/reports' && !hasRole('ADMIN') && !hasPermission('TASK_READ')) {
      next('/')
      return
    }

    if (to.path === '/role-permission-management' && !hasRole('ADMIN')) {
      next('/')
      return
    }

    // 新增的讲师和学生权限检查
    if (to.path.startsWith('/upload-video') && !hasRole('LECTURER') && !hasRole('ADMIN')) {
      next('/')
      return
    }

    if (to.path.startsWith('/manage-categories') && !hasRole('ADMIN')) {
      next('/')
      return
    }

    if (to.path.startsWith('/video-access') && !hasRole('LECTURER') && !hasRole('ADMIN')) {
      next('/')
      return
    }

    // 学习档案页面权限控制：管理员和讲师访问学生积分列表，学生访问个人学习档案
    if (to.path === '/study/profile') {
      if (hasRole('ADMIN') || hasRole('LECTURER')) {
        next('/study/students/points')
        return
      }
    }
  }

  // 允许访问
  next()
})

export default router
